<?php

class Application_Model_LocTreeMapper{

	protected $_dbTable;

	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_LocTree');
		}
		return $this->_dbTable;
	}

	public function save(Application_Model_LocTree $locTree){
		
		$data = array(
				'id_parent'   => $locTree->getParentId(),
				'address' => $locTree->getAddress(),
				'code' => $locTree->getCode(),
		);
		
		$where = array(
				'id_parent = ?'   => $locTree->getParentId(),
				'address = ?' => $locTree->getAddress(),
				'code = ?' => $locTree->getCode(),
				);
		
		$row = new Zend_Db_Table_Row();
		$row = $this->getDbTable()->fetchRow($where);
		if($row == null)
		{
			unset($data['id_loc_tree']);
			return $this->getDbTable()->insert($data);
		}else{
			$this->getDbTable()->update($data, $where);
			return $row->save();
		}
	
	}
	
	public function find($id, Application_Model_LocTree $locTree)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		$locTree->setLocTreeId($row->id_loc_tree);
		$locTree->setParentId($row->id_parent);
		$locTree->setAddress($row->address);
		$locTree->setCode($row->code);
		
		return $locTree;
	}
	
	public function fetchAll($where = null)
	{
		$resultSet = $this->getDbTable()->fetchAll($where);
		$entries   = array();
		foreach ($resultSet as $row) {
			$entry = new Application_Model_LocTree();
			$entry->setLocTreeId($row->id_loc_tree);
			$entry->setParentId($row->id_parent);
			$entry->setCode($row->code);
			$entry->setAddress($row->address);
			
			$entries[] = $entry;
		}
		return $entries;
	}
	
	public function fetchRow($where = null)
	{
		$row = $this->getDbTable()->fetchRow($where);
		$entry = new Application_Model_LocTree();
		if($row != null){
			
			$entry->setLocTreeId($row->id_loc_tree);
			$entry->setParentId($row->id_parent);
			$entry->setCode($row->code);
			$entry->setAddress($row->address);
		}else{
			$entry = null;
		}
		
			
			
		return $entry;
	}
	
// 	public function constructTree($entry, $where, $tree)
// 	{
// 		$resultSet = $this->getDbTable()->fetchAll($where);
		
// 		if(count($resultSet) != 0){
			
// 			foreach ($resultSet as $row) {
				
// 				$entry->setLocTreeId($row->id_loc_tree);
// 				$entry->setParentId($row->id_parent);
// 				$entry->setCode($row->code);
// 				$entry->setAddress($row->address);
// 				$tree[$entry->getLocTreeId()] = $entry->getAddress();
				
// 				$this->constructTree($entry, array('id_parent = ?' => $row->id_loc_tree),$tree);
// 				var_dump($tree);
				
// 			}
// 		}
		
// 	}
	
	//TODO recursif delete
	public function delete($id)
	{
		$result = $this->getDbTable()->find($id);
		if (0 != count($result)) {
			$this->getDbTable()->delete(array('id_loc_tree = ?' => $id));
		}
	
	}
	
}